<template>
	<view>
		<view class="carAddress">
			<text class="address">收货地址：四川省成都市物流区华阳冰河...</text>
			<text class="guanli">管理</text>
		</view>
		<view class="container" v-for="val,index in cartList" :key="index">
			<view class="car-list">
				<!-- 复选框 -->
				<view class="left">
					<label>
						<checkbox :class="{ checked: val.checked }" :checked="val.checked"
							@click="toggleItemCheck(val, index)" />
					</label>
				</view>
				<!-- 商品图片 -->
				<view class="center">
					<img class="car-img" src="./static/logo.png">
				</view>
				<!-- 商品列表 -->
				<view class="right">
					<view class="title">{{val.title}}</view>
					<view class="attr">{{val.attr}}</view>
					<view class="price">¥{{val.price}}</view>
					<view class="functionBox">
						<view @click="addClick(index)" :index="index" @eventChange="numberChange" class="add">+
						</view>
						<view class="number">{{val.number}}</view>
						<view @click="decrease(index)" :index="index" @eventChange="numberChange" class="delete">-
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="probably">
			{{youLike}}
			<div class="item" v-for="(item,index) in youLike" :key="index">
				<image :src="item.image" mode=""></image>
				<view class="car_title">
					<!-- 靓丽买哦 -->
				</view>
				<view class="left_bottom">
					<!-- <text>¥29.90</text>
				<text>¥29.90</text> -->
				</view>
				<view class="right_bottom">

				</view>
			</div>
		</view>
		<view class="bottom">
			<view class="checkboxs">
				<label>
					<checkbox :class="{}" :checked="allChecked" @click="toggleAllCheck()" />全选
				</label>
			</view>
			<view class="total-box">
				<text>不含运费</text>
				<text class="total-price">合计<text>¥{{allPrice}}</text></text>
				<text class="coupon">
				</text>
			</view>
			<view>
				<button class="toPay" @click="toOrders">去结算<text>{{total}}</text></button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				total: 0, // 总数量
				allPrice: 0, //总价格
				testflag: true, // 标识
				allChecked: false, //全选状态
				cartList: [{
						title: '宠物怀念烛台配蜡烛猫狗爪印家合法还发货',
						attr: '120g',
						price: 29.90,
						number: 2,
						checked: false
					},
					{
						title: '宠物怀念烛台配蜡烛猫狗爪印家合法还发货',
						attr: '120g',
						price: 29.90,
						number: 1,
						checked: false
					}
				],
				youLike: []
			}
		},
		onLoad() {
			// this.guessYouLike()
		},
		methods: {
			// 全选状态更新
			updateAllCheckedStatus() {
				this.allChecked = this.cartList.every(item => item.checked);
			},
			// 全选按钮点击事件
			toggleAllCheck() {
				const newStatus = !this.allChecked; // 获取新的全选状态
				this.cartList.forEach(item => item.checked = newStatus);
				this.allChecked = newStatus; // 更新全选状态
			},
			// 单选按钮点击事件
			toggleItemCheck(item) {
				item.checked = !item.checked;
				this.updateAllCheckedStatus(); // 更新全选状态
			},
			addClick(index) {
				if (this.cartList[index].checked) { // 确保商品被选中
					this.cartList[index].number++;
				} else {
					uni.showToast({
						title: '未选中商品无法进行操作',
						icon: 'none'
					});
				}
			},
			//减少
			decrease(index) {
				if (this.cartList[index].checked) { // 确保商品被选中
					if (this.cartList[index].number > 1) {
						this.cartList[index].number--;
					}
				} else {
					uni.showToast({
						title: '未选中商品无法进行操作',
						icon: 'none'
					});
				}
			},
			// 接口
			guessYouLike() {
				uni.request({
					url: 'http://192.168.0.14:8080/exam/getUserFav', //仅为示例，并非真实接口地址。
					data: {
						"pageNum": 1,
						"pageSize": 6
					},
					header: {
						'custom-header': 'hello' //自定义请求头信息
					},
					method: "POST",
					success: (res) => {
						console.log(res.data.data);
					}
				});
			}
		},
		mounted() {
			// uni.setNavigationBarTitle({
			// 	title: '购物车'
			// }),
			this.youLike = this.guessYouLike()
		},
		computed: {
			// 计算总数量
			total() {
				return this.cartList.reduce((sum, item) => sum + (item.checked ? item.number : 0), 0);
			},
			// 计算总价
			allPrice() {
				return this.cartList.reduce((sum, item) => sum + (item.checked ? item.number * item.price : 0), 0).toFixed(
					2);
			},
		}
	}
</script>

<style lang="scss">
	.carAddress {
		height: 80rpx;
		// background: green;
		display: flex;
		flex-direction: column;
		justify-content: center;
		position: relative;

		.address {
			position: absolute;
			display: inline-block;
			font-size: 14px;
			color: #C0C4CC;
			text-indent: 20rpx;
		}

		.guanli {
			position: absolute;
			right: 40px;

		}
	}

	.car-list {
		width: 95%;
		height: 100px;
		margin: 5px auto;
		border-radius: 10px;
		display: flex;
		flex-direction: row;
	}

	.left {
		width: 10%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	checkbox .wx-checkbox-input {
		border-radius: 50%;
		width: 15px;
		height: 15px;
		vertical-align: center;
	}

	.car-img {
		width: 90%;
		height: 90%;
		margin: 5%;
	}

	.center {
		width: 25%;
		height: auto;

	}

	.right {
		width: 65%;
		height: 100%;

		padding: 10px;
		box-sizing: border-box;
	}

	.title {
		font-size: 15px;
		/* font-weight: 600; */
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.attr {
		font-size: 11px;
		/* font-weight: 600; */
		margin: 5px;
		color: #909399;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.price {
		color: #E63F32;
		float: left;
		width: 30%;
	}

	.functionBox {
		float: right;
		width: 70%;

	}

	.delete {
		width: 30px;
		height: 30px;
		line-height: 30px;
		text-align: center;
		border: 1px solid #f5f5f5;
		float: right;
		background: #f5f5f5;
	}

	.number {
		width: 30px;
		height: 30px;
		line-height: 30px;
		text-align: center;
		border: 1px solid #f5f5f5;
		float: right;
		background: #f5f5f5;
	}

	.add {
		width: 30px;
		height: 30px;
		line-height: 30px;
		text-align: center;
		border: 1px solid #f5f5f5;
		float: right;
		background: #f5f5f5;
	}
	
	.probably{
		.item{
			width: 100rpx;
			height: 150rpx;
		}
	}

	.bottom {
		position: fixed;
		left: 0px;
		bottom: 0px;
		z-index: 1000;
		display: flex;
		align-items: center;
		width: 360px;
		height: 52px;
		padding: 0 10px;
		background: rgba(255, 255, 255, 0.9);
		box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.5);
		// border-radius: 8px;

		.checkboxs {
			font-size: 14px;
		}

		.total-box {
			flex: 1;
			display: flex;
			flex-direction: column;
			text-align: right;
			padding-right: 20px;

			.total-price {
				font-size: 16px;
				color: #303133;

				text {
					text-align: right;
					font-weight: bold;
					color: black;
				}
			}

			.coupon {
				font-size: 13px;
				color: #909399;

				text {
					font-weight: bold;
					text-align: right;
					color: green;
				}
			}
		}

		.toPay {
			padding: 0 19px;
			margin: 0;
			border-radius: 100px;
			height: 39px;
			line-height: 39px;
			font-size: 15px;
			color: white;
			background: darkgreen;
		}
	}
</style>